﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPIDesktopClient.Entities;
using PPIProxy;
using System.Data;
using PPIDesktopClient.General;
using VMind.Global;
using PPIMobileInterface;

namespace PPIDesktopClient.Logic
{
    public static class LVendor_Project
    {
        public static EVendor_Project SelectData(DataRow _DataRow)
        {
            EVendor_Project _Vendor_Project = new EVendor_Project();
            _Vendor_Project.ActualCost = ConvertHelper.Todouble(_DataRow["ActualCost"]);
            _Vendor_Project.ActualEndDate = ConvertHelper.ToDateTime(_DataRow["ActualEndDate"]);
            _Vendor_Project.ActualStartDate = ConvertHelper.ToDateTime(_DataRow["ActualStartDate"]);
            _Vendor_Project.Name = ConvertHelper.Tostring(_DataRow["Name"]);
            _Vendor_Project.ScheduledCost = ConvertHelper.Todouble(_DataRow["ScheduledCost"]);
            _Vendor_Project.ScheduledEndDate = ConvertHelper.ToDateTime(_DataRow["ScheduledEndDate"]);
            _Vendor_Project.ScheduledStartDate = ConvertHelper.ToDateTime(_DataRow["ScheduledStartDate"]);
            _Vendor_Project.Status = new EProjectStatus(1);
            _Vendor_Project.Type = new EProjectType(1);
            _Vendor_Project.Vendor = LVendor.SelectData(ConvertHelper.ToInt32(_DataRow["VendorFK"]));
            _Vendor_Project.Vendor_ProjectId = ConvertHelper.ToInt32(_DataRow["Vendor_ProjectId"]);
            return _Vendor_Project;
        }
        public static List<EVendor_Project> SelectAll()
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            DataTable _dt = WVendor_Project.SelectAll(_Reader.GetConnectionString()).Tables[0]; 
            List<EVendor_Project> _VendorProjectList = new List<EVendor_Project>();
            EVendor_Project _VendorProject;
            foreach (DataRow dr in _dt.Rows)
            {
                _VendorProject = new EVendor_Project(ConvertHelper.Toint(dr["Vendor_ProjectID"]), dr["Name"].ToString());
                
                _VendorProject.ActualCost = ConvertHelper.Todouble(dr["ActualCost"]);
                _VendorProject.ActualEndDate = ConvertHelper.ToDateTime(dr["ActualEndDate"]);
                _VendorProject.ActualStartDate = ConvertHelper.ToDateTime(dr["ActualStartDate"]);

                _VendorProject.ScheduledCost = ConvertHelper.Todouble(dr["ScheduledCost"]);
                _VendorProject.ScheduledEndDate = ConvertHelper.ToDateTime(dr["ScheduledEndDate"]);
                _VendorProject.ScheduledStartDate = ConvertHelper.ToDateTime(dr["ScheduledStartDate"]);


                _VendorProject.Vendor = new EVendor(ConvertHelper.ToInt32(dr["VendorFK"]), ConvertHelper.Tostring(dr["Vendor_Name"]),"1");
                _VendorProjectList.Add(_VendorProject);
            }
            return _VendorProjectList;
        }

        public static List<EVendor_Project> SelectAllIdName(DataTable dt)
        {
            List<EVendor_Project> _VendorProjectList = new List<EVendor_Project>();
            EVendor_Project _VendorProject;
            foreach (DataRow dr in dt.Rows)
            {
                _VendorProject = new EVendor_Project(ConvertHelper.Toint(dr["Vendor_ProjectID"]), dr["Name"].ToString());
                _VendorProject.Vendor = new EVendor(ConvertHelper.ToInt32(dr["VendorFK"]));
                _VendorProjectList.Add(_VendorProject);
            }
            return _VendorProjectList;
        }
        public static List<EVendor_Project> SelectAllIdName()
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            DataTable dt = WVendor_Project.SelectAllIdName(_Reader.GetConnectionString()).Tables[0];
            return SelectAllIdName(dt);
        }
        public static List<EVendor_Project> SelectAllIdNameByVendor(int vendorID)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            DataTable dt = WVendor_Project.SelectAllIdNameByVendor(vendorID,_Reader.GetConnectionString()).Tables[0];
            return SelectAllIdName(dt);
        }


        #region Mobile

        public static void InsertProjects(string connectionString)
        {
            InsertProjects(connectionString, Int32.MinValue);
        }

        public static void InsertProjects(string connectionString, int vendorID)
        {

            StringBuilder _query = new StringBuilder();
            List<EVendor_Project> _projects;
            if (vendorID == Int32.MinValue)
                _projects = SelectAllIdName();
            else
                _projects = SelectAllIdNameByVendor(vendorID);
            
            foreach (EVendor_Project p in _projects)
            {
                _query.AppendLine("insert into tb_Projects values (" + p.Vendor_ProjectId + "," + p.Vendor.VendorId + ",'" + p.Name + "')");
                //_query.AppendLine("insert into tb_Projects values (" + p.Vendor_ProjectId + ",'" + p.Name + "')");
            }
            Utils.InsertProject(connectionString, _query.ToString());
        }


        #endregion


    }
}
